home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_2 / menulock / menulock.c < prev    next >
C/C++ Source or Header  |  1991-11-21  |  12KB  |  440 lines

  1. /*-- AutoRev header do NOT edit!
  2. *
  3. *   Program         :   MenuLock.c
  4. *   Copyright       :   © Copyright 1991 Jaba Development
  5. *   Author          :   Jan van den Baard
  6. *   Creation Date   :   11-Sep-91
  7. *   Current version :   1.01
  8. *   Translator      :   DICE v2.6
  9. *
  10. *   REVISION HISTORY
  11. *
  12. *   Date          Version         Comment
  13. *   ---------     -------         ------------------------------------------
  14. *   20-Sep-91     1.01            Now uses the Dos ReadArgs for the Shell.
  15. *   11-Sep-91     1.00            MenuStrip lock utility.
  16. *   11-Sep-91     1.00            Based on code by Stefan Sticht. (thanks!)
  17. *
  18. *-- REV_END --*/
  19.  
  20. /*
  21.  * --- Include a whole buch of headers...
  22.  */
  23. #include <exec/types.h>
  24. #include <exec/memory.h>
  25. #include <dos/dos.h>
  26. #include <libraries/commodities.h>
  27. #include <workbench/startup.h>
  28. #ifndef abs
  29. #define abs
  30. #endif
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <clib/alib_protos.h>
  34. #include <clib/exec_protos.h>
  35. #include <clib/dos_protos.h>
  36. #include <clib/commodities_protos.h>
  37. #include <clib/utility_protos.h>
  38.  
  39. /*
  40.  * --- Macro's to (de)activate the commodity
  41.  */
  42. #define CxOn(b)       ActivateCxObj(b,TRUE);
  43. #define CxOff(b)      ActivateCxObj(b,FALSE);
  44.  
  45. /*
  46.  * --- Calculate a bit-mask from a signal bit.
  47.  */
  48. #define BitMask(b)    ( 1L << ((ULONG)b) )
  49.  
  50. #ifndef NOT
  51. #define NOT !
  52. #endif
  53.  
  54. /*
  55.  * --- Parameters and defaults.
  56.  */
  57. UBYTE *COM_PRIORITY =   "PRIORITY";         /* PRIORITY=<pri>     */
  58. UBYTE *COM_LOCK     =   "LOCK";             /* LOCK=<hotkey>      */
  59. UBYTE *COM_UNLOCK   =   "UNLOCK";           /* UNLOCK=<hotkey>    */
  60. UBYTE *COM_QUIT     =   "QUIT";             /* QUIT=<hotkey>      */
  61.  
  62. WORD   COM_DEF_PRI  =   0;                  /* default priority   */
  63. UBYTE *COM_DEF_LCK  =   "rcommand down";    /* default lock keys  */
  64. UBYTE *COM_DEF_ULC  =   "rcommand up";      /* default unlock keys*/
  65. UBYTE *COM_DEF_QUI  =   "lshift control q"; /* default quit       */
  66.  
  67. #define ML_QUIT         1L                  /* event ID           */
  68.  
  69. /*
  70.  * --- Commodity information
  71.  */
  72. #define ML_VERSION      "v1.01"
  73. #define ML_NAME         "MenuLock"
  74. #define ML_DESCR        "Locks the menu-strip."
  75. #define ML_COPY         "© 1991 Jaba Development"
  76. #define ML_TITLE        ML_NAME " " ML_VERSION ", " ML_COPY
  77.  
  78.  
  79. /*
  80.  * --- Minimum library version required
  81.  */
  82. #define MIN_LIB         36L
  83.  
  84. /*
  85.  * --- Non autoinit libraries required
  86.  */
  87. struct CxBase           *CxBase     = NULL;
  88. struct IconBase         *IconBase   = NULL;
  89.  
  90. extern struct WBStartup *_WBMsg;
  91.  
  92. struct MsgPort          *ML_port    = NULL;    /* Commodity MsgPort */
  93. char                   **ML_tool    = NULL;    /* ToolType array    */
  94. struct RDArgs           *ML_args    = NULL;    /* Shell args        */
  95. struct RDArgs            ML_iargs   = { 0,0,0,0,0,0,0,RDAF_NOPROMPT };
  96.  
  97. ULONG                    ML_arg[4];
  98.  
  99. UBYTE                   *ML_temp    = "PRIORITY/n,LOCK/k,UNLOCK/k,QUIT/k";
  100.  
  101. struct InputEvent       *ML_event;             /* RawMouse event    */
  102. struct InputEvent       *ML_uevent;            /* RawMouse event    */
  103.  
  104. CxObj                   *ML_brok    = NULL;    /* The broker        */
  105.  
  106. /*
  107.  * --- The broker data
  108.  */
  109. struct NewBroker         ML_nbrok = {
  110.     NB_VERSION,ML_NAME,ML_TITLE,ML_DESCR,
  111.     NBU_NOTIFY|NBU_UNIQUE,0,NULL,0
  112. };
  113.  
  114. ULONG                    ML_smask   = NULL;    /* MsgPort signal mask */
  115. UBYTE                   *ML_lstr;              /* lock HotKey string  */
  116. UBYTE                   *ML_ustr;              /* unlock HotKey string*/
  117. UBYTE                   *ML_qstr;              /* quit HotKey string  */
  118.  
  119. /*
  120.  * --- Identification string for the Version CLI-command
  121.  */
  122. UBYTE                   *ML_ver = "\0$VER: MenuLock © 1991 Jaba Development -- v1.01";
  123.  
  124. /*
  125.  * --- Program function proto-types
  126.  */
  127. CxObj  *HotTranslate( UBYTE *, struct InputEvent * );
  128. void    CloseML( long );
  129. void    OpenML( void );
  130. void    CheckML( void );
  131.  
  132. /*
  133.  * --- This might be usefull in your commodities.
  134.  * ---
  135.  * --- This routine set's up a filter using CxFilter and then
  136.  * --- it set's up a translator using CxTranslate. This setup
  137.  * --- allows to directly translate a certan input event into
  138.  * --- another. It's something like "HotKey()" without a Sender
  139.  * --- object and a programmable Translate object.
  140.  * ---
  141.  * --- object = HotTranslate( description, ievent )
  142.  * ---
  143.  * --- CxObj             *object;
  144.  * --- UBYTE             *description;
  145.  * --- struct InputEvent *ievent;
  146.  */
  147. CxObj *HotTranslate( UBYTE *desc, struct InputEvent *ie )
  148. {
  149.     CxObj   *filt, *trans;
  150.  
  151.     if ( NOT( filt  = CxFilter( desc )))
  152.         return( NULL );
  153.     if ( NOT( trans = CxTranslate( ie ))) {
  154.         DeleteCxObj( filt );
  155.         return( NULL );
  156.     }
  157.  
  158.     AttachCxObj( filt, trans );
  159.  
  160.     return( filt );
  161. }
  162.  
  163. /*
  164.  * --- Open up and allocate the resources required
  165.  */
  166. void OpenML( void )
  167. {
  168.     CxObj   *lock, *unlock, *quit;
  169.     WORD     pri;
  170.  
  171.    /*
  172.     * --- Open up non autoinit libraries
  173.     */
  174.     if ( NOT( IconBase = (struct IconBase *)OpenLibrary( "icon.library", MIN_LIB )))
  175.         CloseML( 20L );
  176.     if ( NOT( CxBase = (struct CxBase *)OpenLibrary( "commodities.library", MIN_LIB )))
  177.         CloseML( 20L );
  178.  
  179.     if ( _WBMsg ) {
  180.        /*
  181.         * --- Initialize the ToolTypes array
  182.         */
  183.         ML_tool = (char **)ArgArrayInit( 0L, (char **)_WBMsg );
  184.  
  185.        /*
  186.         * Get new or default priority.
  187.         */
  188.         pri = ArgInt( ML_tool, COM_PRIORITY, COM_DEF_PRI );
  189.  
  190.        /*
  191.         * --- Get new or default HotKey strings
  192.         */
  193.         ML_lstr = ArgString( ML_tool, COM_LOCK, COM_DEF_LCK );
  194.         ML_ustr = ArgString( ML_tool, COM_UNLOCK, COM_DEF_ULC );
  195.         ML_qstr = ArgString( ML_tool, COM_QUIT, COM_DEF_QUI );
  196.     } else {
  197.         if ( ML_arg[0] )    pri = (WORD)ML_arg[0];
  198.         else                pri = COM_DEF_PRI;
  199.         if ( ML_arg[1] )    ML_lstr = (UBYTE *)ML_arg[1];
  200.         else                ML_lstr = COM_DEF_LCK;
  201.         if ( ML_arg[2] )    ML_ustr = (UBYTE *)ML_arg[2];
  202.         else                ML_ustr = COM_DEF_ULC;
  203.         if ( ML_arg[3] )    ML_qstr = (UBYTE *)ML_arg[3];
  204.         else                ML_qstr = COM_DEF_QUI;
  205.     }
  206.  
  207.    /*
  208.     * --- Get a rawmouse input event
  209.     */
  210.     if ( NOT( ML_event = InvertString( "<rawmouse rbutton>", NULL )))
  211.         CloseML( 25L );
  212.     if ( NOT( ML_uevent = InvertString( "<rawmouse rbutton>", NULL )))
  213.         CloseML( 25L );
  214.  
  215.     ML_event->ie_Code  = IECODE_RBUTTON;
  216.     ML_uevent->ie_Code = IECODE_RBUTTON | IECODE_UP_PREFIX;
  217.  
  218.    /*
  219.     * --- commodities.library V36.180 it's InvertString seem's to contain
  220.     * --- a bug which inverts the class "rawmouse" as "event".
  221.     */
  222.     ML_event->ie_Class = ML_uevent->ie_Class = IECLASS_RAWMOUSE;
  223.  
  224.    /*
  225.     * --- Set broker priority
  226.     */
  227.     ML_nbrok.nb_Pri = pri;
  228.  
  229.    /*
  230.     * --- Create a MsgPort
  231.     */
  232.     if ( NOT( ML_port = CreateMsgPort()))
  233.         CloseML( 21L );
  234.  
  235.    /*
  236.     * --- Get the port signalmask and set the port in the broker
  237.     */
  238.     ML_smask         = BitMask(ML_port->mp_SigBit );
  239.     ML_nbrok.nb_Port = ML_port;
  240.  
  241.    /*
  242.     * --- Setup the broker
  243.     */
  244.     if ( NOT( ML_brok = CxBroker( &ML_nbrok, NULL )))
  245.         CloseML( 22L );
  246.  
  247.    /*
  248.     * --- Setup the HotKey filters and attach them to the broker
  249.     */
  250.     if ( NOT( lock = HotTranslate( ML_lstr, ML_event )))
  251.         CloseML( 26L );
  252.     AttachCxObj( ML_brok, lock );
  253.  
  254.     if ( NOT( unlock = HotTranslate( ML_ustr, ML_uevent )))
  255.         CloseML( 26L );
  256.     AttachCxObj( ML_brok, unlock );
  257.  
  258.    /*
  259.     * --- Set up the quit HotKey filter
  260.     */
  261.     if (NOT ( quit = HotKey( ML_qstr, ML_port, ML_QUIT )))
  262.         CloseML( 23L );
  263.     AttachCxObj( ML_brok, quit );
  264.  
  265.    /*
  266.     * --- Check for occured errors
  267.     */
  268.     if ( CxObjError( lock ) || CxObjError( unlock ) || CxObjError( quit ))
  269.         CloseML( 24L );
  270.  
  271.    /*
  272.     * --- If Workbench clear ArgArray
  273.     */
  274.     if ( _WBMsg )
  275.         ArgArrayDone();
  276.  
  277.    /*
  278.     * --- Activate the broker
  279.     */
  280.     CxOn( ML_brok );
  281. }
  282.  
  283. /*
  284.  * --- Close up and de-allocate the used resources
  285.  */
  286. void CloseML( long code )
  287. {
  288.     struct Message  *tmp; /* to clear the MsgPort */
  289.  
  290.    /*
  291.     * --- Delete the broker
  292.     */
  293.     if ( ML_brok )          DeleteCxObjAll( ML_brok );
  294.  
  295.    /*
  296.     * --- Clear and delete the MsgPort
  297.     */
  298.     if ( ML_port ) {
  299.         while (( tmp = GetMsg( ML_port ))) ReplyMsg( tmp );
  300.         DeleteMsgPort( ML_port );
  301.     }
  302.  
  303.    /*
  304.     * --- Free the rawmouse input events
  305.     */
  306.     if ( ML_event )
  307.         FreeIEvents( ML_event );
  308.     if ( ML_uevent )
  309.         FreeIEvents( ML_uevent );
  310.  
  311.    /*
  312.     * --- Free the Shell arguments
  313.     */
  314.     if ( ML_args )
  315.         FreeArgs( ML_args );
  316.  
  317.    /*
  318.     * --- Close the non autoinit libraries
  319.     */
  320.     if ( CxBase )           CloseLibrary( CxBase );
  321.     if ( IconBase )         CloseLibrary( IconBase );
  322.  
  323.    /*
  324.     * --- bye bye
  325.     */
  326.     exit( code );
  327. }
  328.  
  329. /*
  330.  * --- Process the messages coming through the MsgPort
  331.  */
  332. void CheckML( void )
  333. {
  334.     struct Message      *msg;
  335.     struct IntuiMessage *imsg;
  336.     ULONG                sig, type, id;
  337.     BOOL                 running = TRUE;
  338.  
  339.     do {
  340.         sig = Wait( SIGBREAKF_CTRL_C | ML_smask );
  341.  
  342.         if (( sig & SIGBREAKF_CTRL_C ) == SIGBREAKF_CTRL_C ) {
  343.            /*
  344.             * --- Break signal received. Shutting down...
  345.             */
  346.             running = FALSE;
  347.         } else if (( sig & ML_smask) == ML_smask ) {
  348.            /*
  349.             * --- Got a message on the MsgPort. Examine it
  350.             */
  351.             while (( msg = (struct Message *)GetMsg( ML_port ))) {
  352.  
  353.                /*
  354.                 * Get the message type and id
  355.                 */
  356.                 id   = CxMsgID((CxMsg *)msg);
  357.                 type = CxMsgType((CxMsg *)msg);
  358.  
  359.                 ReplyMsg(msg);
  360.  
  361.                 switch( type ) {
  362.  
  363.                     case    CXM_IEVENT:
  364.                         switch( id ) {
  365.  
  366.                             case    ML_QUIT:
  367.                                 running = FALSE;
  368.                                 break;
  369.                         }
  370.                         break;
  371.  
  372.                     case    CXM_COMMAND:
  373.                        /*
  374.                         * --- They want us to do something...
  375.                         */
  376.                         switch( id ) {
  377.  
  378.                             case    CXCMD_UNIQUE:
  379.                                /*
  380.                                 * --- They tried to run a commodity
  381.                                 * --- using our name.
  382.                                 */
  383.                             case    CXCMD_KILL:
  384.                                /*
  385.                                 * --- They want us dead....
  386.                                 */
  387.                                 running = FALSE;
  388.                                 break;
  389.  
  390.                             case    CXCMD_DISABLE:
  391.                                /*
  392.                                 * --- They want us disabled...
  393.                                 */
  394.                                 CxOff( ML_brok );
  395.                                 break;
  396.  
  397.                             case    CXCMD_ENABLE:
  398.                                /*
  399.                                 * --- They want us enabled...
  400.                                 */
  401.                                 CxOn( ML_brok );
  402.                                 break;
  403.                         }
  404.                         break;
  405.                 }
  406.             }
  407.         }
  408.     } while( running == TRUE );
  409.  
  410.    /*
  411.     * --- The end, Fin, Einde, Finito....
  412.     */
  413.     CxOff( ML_brok );
  414. }
  415.  
  416. /*
  417.  * --- Simple main which set's things in motion
  418.  */
  419. void _main( void )
  420. {
  421.     if ( NOT _WBMsg ) {
  422.         setmem( (void *)&ML_arg[0], 16L, 0L );
  423.         if (( ML_args = ReadArgs( ML_temp, &ML_arg[0], &ML_iargs ))) {
  424.             OpenML();
  425.             CheckML();
  426.             CloseML( 0L );
  427.         }
  428.         CloseML(27L);
  429.     } else {
  430.         if ( _WBMsg->sm_ArgList->wa_Lock )
  431.             CurrentDir( _WBMsg->sm_ArgList->wa_Lock );
  432.  
  433.         OpenML();
  434.         CheckML();
  435.         CloseML( 0L );
  436.     }
  437.     atoi(""); /*** otherwise we get a link error and we don't want that ***/
  438.     _waitwbmsg();
  439. }
  440.